package com.xg.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ApplicationPrefs extends Properties {
    private static Log log = LogFactory.getLog(ApplicationPrefs.class);
    public static ApplicationPrefs prefs = null;

    public synchronized static ApplicationPrefs getApplicationPrefs() {
        // if (prefs == null) {
            prefs = new ApplicationPrefs();
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties");
            try {
                prefs.load(is);
            } catch (IOException e) {
                log.error("加载系统配置文件失败!", e);
            }
        // }
        return prefs;
    }

    public static String value(String key) {
        return getApplicationPrefs().getProperty(key);
    }

    public static String value(String key, String defaultvalue) {
        String value = value(key);
        if (StringUtils.hasText(value)) {
            value = defaultvalue;
        }
        return value;
    }

    public static String getString(String key) {
        return getApplicationPrefs().getProperty(key);
    }

    public static Boolean getBoolean(String key) {
        return Boolean.parseBoolean(getString(key));
    }

}
